package com.kevin.test.jmh;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;

@State(Scope.Thread)
public class CopyPropertiesBenchmark {

	private UserModel model = new UserModel();
	private BeanCopier beanCopier = BeanCopier.create(UserModel.class, UserVo.class, false);

	/**
	 * 人工setter复制属性
	 * 
	 * @return
	 */
	@Benchmark
	public UserVo manuallySetter() {
		UserVo vo = new UserVo();
		vo.setId(model.getId());
		vo.setName(model.getName());
		vo.setUserDef1(model.getUserDef1());
		vo.setUserDef2(model.getUserDef2());
		vo.setUserDef3(model.getUserDef3());
		vo.setUserDef4(model.getUserDef4());
		vo.setUserDef5(model.getUserDef5());
		vo.setUserDef6(model.getUserDef6());

		return vo;
	}

	@Benchmark
	public UserVo beanUitls() {
		UserVo vo = new UserVo();
		BeanUtils.copyProperties(this.model, vo);
		return vo;
	}

	@Benchmark
	public UserVo beanCopier() {
		UserVo vo = new UserVo();
		beanCopier.copy(this.model, vo, null);
		return vo;
	}

	public static void main(String[] args) throws RunnerException {
		Options opt = new OptionsBuilder().include(CopyPropertiesBenchmark.class.getSimpleName()).warmupIterations(5)
				.measurementIterations(5).forks(1).build();

		new Runner(opt).run();
	}

}